package com.synnex.mobile.servier.web.content.controller.mobile;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
//import com.synnex.mobile.generic.log.entity.LogEntry;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.servier.core.content.bo.ContentBo;
import com.synnex.mobile.servier.core.updateTime.bo.UpdateTime;
import com.synnex.mobile.servier.core.updateTime.bo.UpdateTimeCriterion;
import com.synnex.mobile.servier.web.content.controller.AbstractContentController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kerryf
 */
@RequestMapping(url = "/mobile/content")
@LogClass(name = "mobile content")
public class MobileContentController extends AbstractContentController {

    private static  String[] EXCLUDE_PROPERTIES = new String[]{"updateDate","entryDate"};

    @RequestMapping(url = "/introduce.do")
    @LogMethod(name = "get company introduce")
    public ModelAndView getIntro(HttpServletRequest request, HttpServletResponse response) {
        try {

            String updateTimeStr = request.getParameter("updateTime");

            EXCLUDE_PROPERTIES = new String[]{"updateDate","entryDate","day","group","groupId","groupName","section","seq","hotelAddr","hotelName","hotelOther","id","picPath"};
            JSONModel jsonModel = JSONModel.create().setExcludes(EXCLUDE_PROPERTIES);

            Long returnTime = null;
            UpdateTimeCriterion criterion = new UpdateTimeCriterion();
            criterion.setUrl(super.getIntroUrl(request));

            Long updateTime = null;
            if (null != updateTimeStr && !"".equals(updateTimeStr)) {
                updateTime = Long.parseLong(updateTimeStr);
//                criterion.setUpdateTime(updateTime);
            }
            UpdateTime updateTimeFromDB = updateTimeService.getUpdateTime(criterion);

            UpdateTime cache = new UpdateTime();

            if (null == updateTimeFromDB) {
                Long newTime = System.currentTimeMillis();
                cache.setUpdateTime(newTime);
                cache.setUrl(getIntroUrl(request));
                updateTimeService.saveUpdateTime(cache);

                ContentBo introduce = super.getIntroduce(request);
                jsonModel.setData(introduce);
                jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                returnTime = newTime;
            } else {
                if (updateTime == null || updateTimeFromDB.getUpdateTime().longValue() != updateTime.longValue()) {
                    returnTime = updateTimeFromDB.getUpdateTime();

                    jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                    ContentBo introduce = super.getIntroduce(request);
                    jsonModel.setData(introduce);
                } else {
                    jsonModel.setStatus(JSONModel.STATUS.CACHEED);
                    returnTime = updateTimeFromDB.getUpdateTime();
                }
            }
            jsonModel.addMoreProperty("updateTime", returnTime.toString());
            System.out.println(jsonModel);
            return createJSONModelAndView(jsonModel);

        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }



    @RequestMapping(url = "/getTour.do")
    @LogMethod(name = "get meeting round")
    public ModelAndView getTour(HttpServletRequest request, HttpServletResponse response) {
//        LogEntry log = this.getMobileSession(request).getCurrentLogEntry(request);
        try {

            String updateTimeStr = request.getParameter("updateTime");

            EXCLUDE_PROPERTIES = new String[]{"updateDate","entryDate","day","group","groupId","groupName","section","id","hotelAddr","hotelName","hotelOther","title"};
            JSONModel jsonModel = JSONModel.create().setExcludes(EXCLUDE_PROPERTIES);

            Long returnTime = null;
            UpdateTimeCriterion criterion = new UpdateTimeCriterion();
            criterion.setUrl(getUrl(request));

            Long updateTime = null;
            if (null != updateTimeStr && !"".equals(updateTimeStr)) {
                updateTime = Long.parseLong(updateTimeStr);
//                criterion.setUpdateTime(updateTime);
            }
            UpdateTime updateTimeFromDB = updateTimeService.getUpdateTime(criterion);

            UpdateTime cache = new UpdateTime();

            if (null == updateTimeFromDB) {
                Long newTime = System.currentTimeMillis();
                cache.setUpdateTime(newTime);
                cache.setUrl(getUrl(request));
                updateTimeService.saveUpdateTime(cache);

//                ContentBo introduce = super.getContentBo(request);
                Map<String,Object> contentBoList = super.getContentBoListByType(request);
                jsonModel.setData(contentBoList);
                jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                returnTime = newTime;
            } else {
                if (updateTime == null || updateTimeFromDB.getUpdateTime().longValue() != updateTime.longValue()) {
                    returnTime = updateTimeFromDB.getUpdateTime();

                    jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                    Map<String,Object> contentBoList = super.getContentBoListByType(request);
                    jsonModel.setData(contentBoList);
                } else {
                    jsonModel.setStatus(JSONModel.STATUS.CACHEED);
                    returnTime = updateTimeFromDB.getUpdateTime();
                }
            }
            jsonModel.addMoreProperty("updateTime", returnTime.toString());

//            log.setItemText("type: "+request.getParameter("type"));
            return createJSONModelAndView(jsonModel);

        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }


    @RequestMapping(url = "/sessionList.do")
    @LogMethod(name = "get session By group")
    public ModelAndView getSessions(HttpServletRequest request, HttpServletResponse response) {
//        LogEntry log = this.getMobileSession(request).getCurrentLogEntry(request);
        try {

            String updateTimeStr = request.getParameter("updateTime");

            EXCLUDE_PROPERTIES = new String[]{"updateDate","entryDate","day","group","section","groupId","id","groupName","seq","type"};
            JSONModel jsonModel = JSONModel.create().setExcludes(EXCLUDE_PROPERTIES);

            Map<String,Object> data = new HashMap<String,Object>();

            Long returnTime = null;
            UpdateTimeCriterion criterion = new UpdateTimeCriterion();
            criterion.setUrl(getSessionUrl(request));

            Long updateTime = null;
            if (null != updateTimeStr && !"".equals(updateTimeStr)) {
                updateTime = Long.parseLong(updateTimeStr);
//                criterion.setUpdateTime(updateTime);
            }
            UpdateTime updateTimeFromDB = updateTimeService.getUpdateTime(criterion);

            UpdateTime cache = new UpdateTime();

            if (null == updateTimeFromDB) {
                Long newTime = System.currentTimeMillis();
                cache.setUpdateTime(newTime);
                cache.setUrl(getSessionUrl(request));
                updateTimeService.saveUpdateTime(cache);

                List<ContentBo> sessions = super.getSessionsByDayAndSection(request);
                data.put("results",sessions);

                jsonModel.setData(data);
                jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                returnTime = newTime;
            } else {
                if (updateTime == null || updateTimeFromDB.getUpdateTime().longValue() != updateTime.longValue()) {
                    returnTime = updateTimeFromDB.getUpdateTime();
                    jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                    List<ContentBo> sessions = super.getSessionsByDayAndSection(request);

                    data.put("results",sessions);
                    jsonModel.setData(data);
                } else {
                    jsonModel.setStatus(JSONModel.STATUS.CACHEED);
                    returnTime = updateTimeFromDB.getUpdateTime();
                }
            }
            jsonModel.addMoreProperty("updateTime", returnTime.toString());

//            log.setItemText("day"+request.getParameter("day")+" , section: "+request.getParameter("section"));
            return createJSONModelAndView(jsonModel);

        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }



}
